package com.mail.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.mail.bean.EmailInfo;
import com.mail.dao.IEmailInfoDao;

/**
 * <描述>
 * 
 * @author heem
 * @since version 1.0.0
 * @copyright
 * @initialize <实例化方法>
 * @date 2014-6-2
 * @log <重大修改日志，格式：YYYY-MM-DD 修改人 修改内容>
 */
@Component
public class EmailInfoDaoImpl implements IEmailInfoDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void save(String[] values) {
        String sql = "INSERT INTO " + EmailInfo.TABLE_NAME + "(ADDRESS,NAME,PASSWORD) VALUES(?,?,?)";
        try {
            jdbcTemplate.update(sql, values[0], values[1], values[2]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<EmailInfo> queryEmail(String address) {
        String sql = "SELECT * FROM  " + EmailInfo.TABLE_NAME + " WHERE ADDRESS LIKE '%" + address + "'";
        List<Map<String, Object>> listMap = jdbcTemplate.queryForList(sql);
        List<EmailInfo> list = new ArrayList<EmailInfo>();
        for (Map<String, Object> map : listMap) {
            EmailInfo ei = new EmailInfo();
            ei.setAddress(String.valueOf(map.get("ADDRESS")));
            ei.setName(String.valueOf(map.get("NAME")));
            ei.setPassword(String.valueOf(map.get("PASSWORD")));
            list.add(ei);
        }
        return list;
    }

    public void saveCanLoginAccount(EmailInfo emailInfo) {
        String sql = "INSERT INTO lOGINMAIL(ADDRESS,NAME,PASSWORD) VALUES(?,?,?)";
        try {
            jdbcTemplate.update(sql, emailInfo.getAddress(), emailInfo.getName(), emailInfo.getPassword());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
